#include <iostream>
#include <stdio.h>
#include "html_globals.h"
#include "html_parser.h"
html_parser parser;
html_document* doc;


int main()
{
    char* buffer = 0;
    FILE* f = 0;
    f = fopen("tests/google.html","r");

    if (!f) return false;

    fseek(f,0,SEEK_END);
    unsigned int size = ftell(f);
    fseek(f,0,SEEK_SET);

    buffer = new char[size+1];
    memset(buffer,0,size+1);

    fread(buffer,size,1,f);

    fclose(f);

    doc = new html_document();
    parser.parse_go(doc,buffer); // pass in the html_document object and the buffer

    delete [] buffer;

     // test parsing

    html_tag *c = 0;
    for (int i =0;i< (int)doc->html_t.size();i++)
    {
        c = doc->html_t[i];
        if (!c) break;

        printf("\nname = %s",c->name.c_str());
        printf("\ntext = %s",c->text.c_str());

        html_attrib* a = c->first_child;
        if (a)
        {

            printf("\n-- attributes\n");


            while(a)
            {
                printf("---- name = %s\n",a->name.c_str());
                printf("---- value = %s\n",a->value.c_str());

                a = a->next;
            }
        }else{ printf("\nNo attributes\n"); }

        html_tag* t = c->parent_tag;
        if (t)
        {

            printf("\n------ Parent\n");


            while(t)
            {
                printf("------- name = %s\n",t->name.c_str());

                t = t->parent_tag;
            }
        }else{ printf("\nNo Parents\n"); }


    }

    delete doc;

    return 0;
}
